package com.tegtech.starter.security.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import com.tegtech.common.domain.Result;
import com.tegtech.common.utils.http.ServletUtils;
import com.tegtech.module.auth.domain.LoginUser;
import com.tegtech.starter.security.service.TokenService;

import cn.hutool.core.lang.Validator;

/**
 * 自定义退出处理类 返回成功
 * 
 * @author Jun
 */
@Configuration
public class SysLogoutSuccessHandler implements LogoutSuccessHandler {
	
	@Autowired
	private TokenService tokenService;

	/**
	 * 退出处理
	 * 
	 * @return
	 */
	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		LoginUser loginUser = tokenService.getLoginUser(request);
		if (Validator.isNotNull(loginUser)) {
			// 删除用户缓存记录
			tokenService.delLoginUser(loginUser.getToken());
		}
		ServletUtils.reponseData(response, Result.ok());
	}
}
